package cn.diancan.action.manager;


import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.manager.security.ManagerAuthUtil;
import cn.diancan.model.SysManager;
import cn.diancan.util.VerifyCodeUtil;
import cn.diancan.vo.other.VerifyCodeVo;
/**
 * 后台登陆管理
 * @author Administrator
 */
public class LoginAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

    /**
     * 
     * @return
     */
    public String toLogin() {
	/*	String mobileSrc= getRequest().getParameter("mr");
		if( null != mobileSrc && "mobile".equals(mobileSrc)){

			return freemarkView("/views/ftl/manager/mobile_login.ftl");
		}

		return freemarkView("/views/ftl/manager/login.ftl");*/
		return index1();
    }


    public String index1(){
		try {
			SysManager manager = sysManagerService.findLogined("root", "xiaofang");
			if (null == manager) {
				return jsonError("用户名或密码错误!");
			}
			ManagerAuthUtil.setValue(manager.getId());
			ManagerAuthUtil.setBean(manager);
		} catch (Exception e) {

		}
		return freemarkView("/views/ftl/manager/index.ftl");
	}
	
	 
    /**
     * 后台首页
     * 
     * @return
     */
    @ManagerAuth
    public String index() {

		return freemarkView("/views/ftl/manager/index.ftl");
    }

    /**
     * 手机登录后台页面 
     */
    @ManagerAuth
    public String indexMobile() {
	return freemarkView("/views/ftl/manager/mobile_index.ftl");
    }
    
    /**
     * 管理员登入验证
     * @return
     */
    public String login() {
	try {
	   
	    Object sesssionCode =  getSession().getAttribute("managerLoginVerifyCode");
	    String verifyCode = getRequest().getParameter("verifyCode");
	    try {
		System.out.println("verifyCode"+verifyCode);
		System.out.println("sessionCode"+sesssionCode); 
	    } catch (Exception e) { 
		e.printStackTrace();
	    }
	   
	     if(null==sesssionCode || null == verifyCode 
		    || !sesssionCode.toString().equalsIgnoreCase(verifyCode) ){ 
		return jsonError("验证码错误");
	    } 
	    
	    String managerName = getRequest().getParameter("userName");
	    String psw = getRequest().getParameter("passWord");
	    if (null == managerName) {
		return jsonError("请填写用户名");
	    }
	    if (null == psw) {
		return jsonError("请填写密码"); 
	    }
	    SysManager manager = sysManagerService.findLogined(managerName, psw);
	    if (null == manager) {
		return jsonError("用户名或密码错误!");  
	    }
	    ManagerAuthUtil.setValue(manager.getId());
	    ManagerAuthUtil.setBean(manager);
	    getSession().removeAttribute("managerLoginVerifyCode");
	    // 登录成功 
	    return jsonSuccess("登录成功");
	} catch (OutMsgException e) {
	    return jsonError(e);
	}catch (Exception e) {
	    e.printStackTrace();
	}
	return jsonError("登录失败!");
    }
    
    public String verifyCode() {
	try {
	    VerifyCodeVo verifyCodeVo = VerifyCodeUtil.getColorCode();
	    getSession().setAttribute("managerLoginVerifyCode",verifyCodeVo.getCode());
	    getResponse().setContentType("image/png");
	    getResponse().setContentLength(verifyCodeVo.getImageCode().length);
	    getResponse().getOutputStream().write(verifyCodeVo.getImageCode());
	    getResponse().getOutputStream().flush();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }
}
